package com.goal98



import grails.converters.XML
import grails.converters.JSON
import com.goal98.exception.ResourceNotFoundException

class TeamPostController {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    //static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        def model = [ teamPostInstanceList: TeamPost.list( params ), teamPostInstanceTotal: TeamPost.count() ]
        withFormat{
            html{
                return model
            }
            xml{
                render model as XML
            }
            json{
                render model as JSON
            }
        }


    }



    def edit = {
        def teamPostInstance = TeamPost.get( params.id )

        if(!teamPostInstance) {
            flash.message = "TeamPost not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ teamPostInstance : teamPostInstance ]
        }
    }



    def create = {
        def teamPostInstance = new TeamPost()
        teamPostInstance.properties = params
        return ['teamPostInstance':teamPostInstance]
    }


 /**
    * rest actions
  */

    def show = {
        def teamPostInstance = TeamPost.get( params.id )

        if(!teamPostInstance) {
            final  notFoundMessage = "TeamPost not found with id ${params.id}"
            withFormat{
                html{

                    flash.message = notFoundMessage
                    redirect(action:list)
                }
                xml{
                    response.status = 404
                    render(view:"/errorxml",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(notFoundMessage)])
                }
                json{
                    response.status = 404
                    render(view:"/errorjson",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(notFoundMessage)])
                }
            }

        }
        else {
            withFormat{
                html{
                    return [ teamPostInstance : teamPostInstance ]
                }
                xml{
                    render teamPostInstance as XML
                }
                json{
                    render teamPostInstance as JSON
                }
            }

        }
    }

    def save = {
        def bindData = params.teampost?params.teampost:params
        def teamPostInstance = new TeamPost(bindData)
        if(!teamPostInstance.hasErrors() && teamPostInstance.save()) {
            final String createSucessMsg = "TeamPost ${teamPostInstance.id} created"
            withFormat{
                form{
                    flash.message = createSucessMsg
                    redirect(action:show,id:teamPostInstance.id)
                }
                xml{
                    response.status = 201 //created
                    render teamPostInstance as XML
                }
                json{
                    response.status = 201 //created
                    render teamPostInstance as JSON
                }
            }


        }
        else {
            withFormat{
                form{
                    render(view:'create',model:[teamPostInstance:teamPostInstance])
                }
                xml{
                    response.status = 500
                    render teamPostInstance.errors as XML
                }
                json{
                    response.status = 500
                    render teamPostInstance.errors as JSON
                }
            }

        }
    }

    def update = {
        def teamPostInstance = TeamPost.get( params.id )
        if(teamPostInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(teamPostInstance.version > version) {
                    withFormat{
                        form{
                            
                            teamPostInstance.errors.rejectValue("version", "teamPost.optimistic.locking.failure", "Another user has updated this TeamPost while you were editing.")
                            render(view:'edit',model:[teamPostInstance:teamPostInstance])
                        }

                        xml{
                            response.status = 500
                            render teamPostInstance.errors as XML
                        }
                        json{
                            response.status = 500
                            render teamPostInstance.errors as JSON
                        }

                    }

                }
            }
            teamPostInstance.properties = params
            if(!teamPostInstance.hasErrors() && teamPostInstance.save()) {
                final String updateSuccessMsg = "TeamPost ${params.id} updated"
                withFormat{
                    form{
                        flash.message = updateSuccessMsg
                        redirect(action:show,id:teamPostInstance.id)
                    }
                    xml{
                        render teamPostInstance as XML
                    }
                    json{
                        render teamPostInstance as JSON
                    }
                }


            }
            else {
                withFormat{
                    form{
                        render(view:'edit',model:[teamPostInstance:teamPostInstance])
                    }
                    xml{
                        response.status = 500
                        render teamPostInstance.errors as XML
                    }
                    json{
                        response.status = 500
                        render teamPostInstance.errors as JSON
                    }
                }

            }
        }
        else {
            final String updateNotFoundMsg = "TeamPost not found with id ${params.id}"
            withFormat{
                form{
                    flash.message = updateNotFoundMsg
                    redirect(action:edit,id:params.id)
                }
                xml{
                    response.status = 404
                    render(view:"/errorxml",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(updateNotFoundMsg)])
                }
                json{
                    response.status = 404
                    render(view:"/errorjson",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(updateNotFoundMsg)])
                }
            }

        }
    }

    def delete = {
        def teamPostInstance = TeamPost.get( params.id )

        if(teamPostInstance) {
            try {
                final String delSuccessMsg = "TeamPost ${params.id} deleted"
                teamPostInstance.delete()
                withFormat{
                    form{
                        flash.message = delSuccessMsg
                        redirect(action:list)
                    }
                    xml{
                        render delSuccessMsg
                    }
                    json{
                        render delSuccessMsg
                    }
                }

            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                final String delFailMsg = "TeamPost ${params.id} could not be deleted"
                withFormat{
                    form{
                        flash.message = delFailMsg
                        redirect(action:show,id:params.id)
                    }
                    xml{
                        response.status = 409
                        render(view:"/errorxml",
                                model:[errorCode:"409",
                                        exception:new ResourceNotFoundException(delFailMsg)])
                    }
                    json{
                        response.status = 409
                        render(view:"/errorjson",
                                model:[errorCode:"409",
                                        exception:new ResourceNotFoundException(delFailMsg)])
                    }
                }
            }
        }
        else {
            final String delNotFoundMsg = "TeamPost not found with id ${params.id}"
            withFormat{
                form{
                    flash.message = delNotFoundMsg
                    redirect(action:list)
                }
                xml{
                    response.status = 404
                    render(view:"/errorxml",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(delNotFoundMsg)])
                }
                json{
                    response.status = 404
                    render(view:"/errorjson",
                            model:[errorCode:"404",
                                    exception:new ResourceNotFoundException(delNotFoundMsg)])
                }
            }


        }
    }
}
